﻿using System;
using System.Collections.Generic;
using System.Text;
using Cn.Cellula.Biz.Port;
using Cn.Cellula.Model;
using System.Web.UI.WebControls;
using System.Reflection;
using Cn.Cellula.Model.System;

namespace Cn.Cellula.Biz.Impl {
    class CodeBiz : MyBizSupport, ICodeBiz {
        #region ICodeBiz 成员

        public void DeleteCodeType(IList<String> rKeys) {
            IList<TSysCodeValueEO> list = new List<TSysCodeValueEO>();
            TSysCodeTypeEO eo = null;
            try {
                foreach (String k in rKeys) {
                    eo = base.Load<TSysCodeTypeEO>(k);
                    if (eo.IsLocked == 0) {
                        base.Delete(" from TSysCodeValueEO eo where eo.TypeKey='" + k + "' ");
                        base.Delete<TSysCodeTypeEO>(eo);
                    }
                }
            } catch(Exception ex) {
                log.Error(ex.Message);
            }
        }

        public IList<TSysCodeValueEO> LoadCodeByType(string rTypeKey) {
            String hql = " from TSysCodeValueEO eo where eo.TypeKey='" + rTypeKey + "' order by eo.OrderNumber desc ";
            return base.Find<TSysCodeValueEO>(hql);
        }

        public Int32 AddCodeType(TSysCodeTypeEO eo) {
            String hql = " from TSysCodeTypeEO eo where eo.TypeName='" + eo.TypeName + "' ";
            IList<TSysCodeTypeEO> _eos = base.Find<TSysCodeTypeEO>(hql);
            if (_eos != null && _eos.Count > 0) {
                return 1;     
            }
            base.Save<TSysCodeTypeEO>(eo);
            return 0;
        }

        public int UpdateCodeType(TSysCodeTypeEO rNewEO, String rOldTypeKey) {
            String hql = " update TSysCodeValueEO eo set eo.TypeKey='" + rNewEO.TypeKey + "' where eo.TypeKey='" + rOldTypeKey + "' ";
            base.ExecuteUpdate(hql);
            hql = " update TSysCodeTypeEO eo set eo.TypeKey='" + rNewEO.TypeKey + "' "
                + " ,eo.TypeName='"+rNewEO.TypeName+"' "
                + " ,eo.IsLocked=" + rNewEO.IsLocked+ " "
                + " where eo.TypeKey='" + rOldTypeKey + "' ";
            return base.ExecuteUpdate(hql);
            
        }
        public int UpdateCodeValue(TSysCodeValueEO rNewEO, String rOldCodeKey) {
            String hql = " update TSysCodeValueEO eo set eo.CodeKey='" + rNewEO.CodeKey + "' "
                + " ,eo.CodeName='"+rNewEO.CodeName+"' "
                + " ,eo.TypeKey='"+rNewEO.TypeKey+"' "
                + " ,eo.OrderNumber=" + rNewEO.OrderNumber
                + " where eo.CodeKey='" + rOldCodeKey + "' ";
            return base.ExecuteUpdate(hql);

        }
        public void DeleteCodeValue(IList<string> rKeys) {
            TSysCodeValueEO eo = null;
            foreach (String k in rKeys) {
                eo = base.Load<TSysCodeValueEO>(k);
                base.Delete<TSysCodeValueEO>(eo);
            }
        }


        public IList<TSysCodeTypeEO> LoadCodeTypeByName(string rName) {
            String hql = " from TSysCodeTypeEO eo where eo.TypeName='" + rName + "' ";
            return base.Find<TSysCodeTypeEO>(hql);
        }

        public IList<TSysCodeValueEO> LoadCodeValueByName(string rName) {
            String hql = " from TSysCodeValueEO eo where eo.CodeName='" + rName + "' ";
            return base.Find<TSysCodeValueEO>(hql);
        }

        public bool IfTypeNameExist(string rName) {
            IList<TSysCodeTypeEO> list = LoadCodeTypeByName(rName);
            return (list != null && list.Count > 0);
        }

        public bool IfCodeNameExist(string rName) {
            IList<TSysCodeValueEO> list =  LoadCodeValueByName(rName);
            return (list != null && list.Count > 0);
        }

        public IList<ListItem> LoadCode2ListItem(String rTypeKey, Boolean rSelected) {
            IList<TSysCodeValueEO> list = this.LoadCodeByType(rTypeKey);
            IList<ListItem> lis = new List<ListItem>();
            if (!rSelected) {
                lis.Add(new ListItem("", ""));
            }
            foreach (TSysCodeValueEO eo in list) {
                ListItem li = new ListItem();
                li.Text = eo.CodeName;
                li.Value = eo.CodeKey;
                lis.Add(li);
            }
            return lis;
        }

        public void SetTypeLock(IList<string> keys, bool rLock) {
            foreach (String k in keys) {
                TSysCodeTypeEO eo = base.Load<TSysCodeTypeEO>(k);
                eo.IsLocked = (rLock) ? 1 : 0;
                base.Update<TSysCodeTypeEO>(eo);
            }
        }
        public String LoadCode2JskitDdl(String rTypeKey, String rDefaultKey) {
            try {
                StringBuilder sb = new StringBuilder();
                sb.Append("[");
                String hql = "from TSysCodeValueEO eo where eo.TypeKey='" + rTypeKey + "'";
                IList<TSysCodeValueEO> list = base.Find<TSysCodeValueEO>(hql);
                if (list != null && list.Count > 0) {
                    for (int i=0;i<list.Count;i++) {
                        TSysCodeValueEO eo = list[i];
                        if (i > 0)
                            sb.Append(',');
                        sb.Append("[\""+eo.CodeKey+"\",\""+eo.CodeName+"\"]");
                    }
                }
                sb.Append("]");
                return sb.ToString();
            } catch (Exception ex) {
                log.Error(ex.Message);
                return "[]";
            }
        }
        #endregion
    }
}
